package br.com.andreikeda.utility.gestor.finance.storage.database.dao;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

import br.com.andreikeda.utility.gestor.finance.storage.database.model.Dividend;
import br.com.andreikeda.utility.gestor.finance.utils.ColumnConstants;

/**
 * Created by ikeda on 22/01/15.\n\n
 * Contact: <b>andreikeda87@gmail.com</b> or <b>andre.ikeda@99motos.com</b>
 */
public class DividendDAO extends GenericDAO {

    private static DividendDAO instance;

    private final String TABLE = "DIVIDEND";
    private final String WHERE_CLAUSE_DEFAULT = "id=?";

    public static DividendDAO getInstance() {
        if (instance == null)
            instance = new DividendDAO();
        return instance;
    }

    @Override
    public boolean delete(SQLiteDatabase mLite, Object object) {
        if (!(object instanceof Dividend))
            return false;
        return mLite.delete(TABLE, WHERE_CLAUSE_DEFAULT, new String[]{String.valueOf((((Dividend) object).getId()))}) > 0;
    }

    @Override
    public void deleteAll(SQLiteDatabase mLite, String whereClause, String[] whereArgs) {
        mLite.delete(TABLE, whereClause, whereArgs);
    }

    @Override
    public boolean insert(SQLiteDatabase mLite, Object object) {
        if (!(object instanceof Dividend))
            return false;
        ContentValues cv = new ContentValues();
        cv.put(ColumnConstants.CODE, ((Dividend) object).getCode());
        cv.put(ColumnConstants.GROSS_AMOUNT, ((Dividend) object).getGrossAmount());
        cv.put(ColumnConstants.NET_BALANCE, ((Dividend) object).getNetBalance());
        mLite.insert(TABLE, null, cv);
        return true;
    }

    @Override
    public List<Object> list(SQLiteDatabase mLite) {
        Cursor c = mLite.query(TABLE, null, null, null, null, null, String.format("ORDER BY %s DESC", ColumnConstants.CREATED_AT));
        List dividends = new ArrayList<Dividend>();
        if (c.moveToFirst()) {
            while (!c.isAfterLast()) {
                dividends.add(
                        new Dividend(c.getInt(c.getColumnIndex(ColumnConstants.ID)),
                                c.getString(c.getColumnIndex(ColumnConstants.CODE)),
                                c.getString(c.getColumnIndex(ColumnConstants.GROSS_AMOUNT)),
                                c.getString(c.getColumnIndex(ColumnConstants.NET_BALANCE)),
                                c.getString(c.getColumnIndex(ColumnConstants.CREATED_AT))));
                c.moveToNext();
            }
        }
        return dividends;
    }

    @Override
    public List<Object> listFrom(SQLiteDatabase mLite, String whereClause, String[] whereArgs) {
        Cursor c = mLite.query(TABLE, null, whereClause, whereArgs, null, null, String.format("ORDER BY %s DESC", ColumnConstants.CREATED_AT));
        List dividends = new ArrayList<Dividend>();
        if (c.moveToFirst()) {
            while (!c.isAfterLast()) {
                dividends.add(
                        new Dividend(c.getInt(c.getColumnIndex(ColumnConstants.ID)),
                                c.getString(c.getColumnIndex(ColumnConstants.CODE)),
                                c.getString(c.getColumnIndex(ColumnConstants.GROSS_AMOUNT)),
                                c.getString(c.getColumnIndex(ColumnConstants.NET_BALANCE)),
                                c.getString(c.getColumnIndex(ColumnConstants.CREATED_AT))));
                c.moveToNext();
            }
        }
        return dividends;
    }

    @Override
    public boolean update(SQLiteDatabase mLite, Object object) {
        if (!(object instanceof Dividend))
            return false;
        ContentValues cv = new ContentValues();
        cv.put(ColumnConstants.GROSS_AMOUNT, ((Dividend) object).getGrossAmount());
        cv.put(ColumnConstants.NET_BALANCE, ((Dividend) object).getNetBalance());
        mLite.update(TABLE, cv, WHERE_CLAUSE_DEFAULT, new String[]{String.valueOf(((Dividend) object).getId())});
        return true;
    }

    @Override
    public void updateAll(SQLiteDatabase mLite, ContentValues cv, String whereClause, String[] whereArgs) {
        mLite.update(TABLE, cv, whereClause, whereArgs);
    }

    private DividendDAO() {

    }
}
